﻿<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../js/jquery.min.js"></script>
    <script src="../js/jquery.easyui.min.js"></script>
    <script src="../js/easyui-lang-zh_CN.js"></script>
    <link rel="stylesheet" href="../themes/icon.css">
    <link rel="stylesheet" href="../themes/default/easyui.css">
    <link rel="stylesheet" href="../css/wu.css">
    <script>
        var  selectIndex = -1;

        function hideTable(){
            $("#searchdiv").toggle();
        }
        function total(col){
        	var rows=$("#partList").datagrid('getRows');
        	var total=0;
        	for(var i=0;i<rows.length;i++){
        		total+=parseFloat(rows[i][col]);
        	}
        	return total;
        }
		function flush(){
       		$("#productList").datagrid('reload');
       	}
        	
        $(function(){
            $("#productList").datagrid({
               /*  fit:true, */
				url:'/kxmanage/offerPriceBill/getAllByCondition',
             	 /*   url:'/test.json', */
				pageNumber:1,
				pageSize:5,
				pageList:[5,10],
				pafination:true,
				checkOnSelect:false,
                columns:[[
                    {field:'ck',checkbox:true},
                    {field:'code',title:'报价编号',width:140,
                    	formatter:function(value,row,index){
                    		return '<a name="billCode" onclick="changeProduct(this)">'+value+'</a>';
                    	}
                    },
                    {field:'offerTime',title:'报价日期',width:120},
                    {field:'customName',title:'客户名称',width:150},
                    {field:'money',title:'总货值',width:60},
                    {field:'linkname',title:'联系人',width:100},
                    {field:'phone',title:'联系方式',width:100},
                    {field:'checkStatus',title:'审核状态',width:110,
                    	formatter:function(value,row,index){
                    		if(value=="未审核"){
                    			return '<span style="color:red;">未审核</span>';
                    		}else{
                    			return value;
                    		}
                    	}},
                    {field:'operator',title:'操作员',width:100,
                    	formatter:function(value,row,index){
                    		return '${user.userName}';
                    	}
                    },
					{field:'opt',title:'操作',width:200,formatter:function (value,row,index) {
						var context="<a onclick='changeProduct(this)'>修改</a>&nbsp;&nbsp;" +
							"<a onclick='delRows(this)'>删除</a>";
						return context;
					}}
                ]],
                toolbar:'#wu_toolbar',
                pagination:true ,//向后台传递参数page 和rows
                onClickRow: function(index, data) {
                    $(this).datagrid('unselectRow', index);
                },
                onDblClickRow : function(index, row) {
	                $("#show").show();
	                $("#detailed").html("<h3>单据标号为：<span>"+row.code+"</span> 的明细如下所列<h3>");
	                $("#partList").datagrid({
		                url:'/kxmanage/offerPriceBill/getOfferPriceBillDetail',
		                queryParams:{code:row.code},
		                checkOnSelect:true,
		                rownumbers:true,
		                columns:[[
		                    {field:'code',title:'配件编号',width:130},
		                    {field:'partName',title:'配件名称',width:200},
		                    {field:'partBrand',title:'配件品牌',width:120},
		                    {field:'partNo',title:'配件型号',width:100},
		                    {field:'amount',title:'数量',width:100},
		                    {field:'sellPrice',title:'单价',width:100, 
		                        formatter:function(value,row,index){
		                        	if(row.code!=''&&row.code!=null)
		                          	  return "￥"+row.sellPrice;
		                            else return null;
		                        }
		                    },
		                    {field:'price',title:'总金额',width:100,
		                         formatter:function(value,row,index){
		                            return "￥"+value;
		                        }
		                    },
		                    {field:'memo',title:'备注',width:210},
		                    {field:'opt',title:'操作',width:100,formatter:function (value,row,index) {
		                    	if(row.code!=''&&row.code!=null){
		                            var context="<a style='opacity: 0.3;'>删除</a>&nbsp;&nbsp;"; 
		                            return context;
		                    	}
		                        else return null;
		                        }}
		
		                ]],
						onLoadSuccess:function(){
							$("#partList").datagrid('appendRow',{
								'partBrand':"合计",
								'amount':total('amount'),
								'price':total('price')
							});
						},
						onClickRow: function(index, data) {
							$(this).datagrid('unselectRow', index);
						}
	                  });
	                }
            });
             $("#reset_btn").click(function(){
             	$("#form1").form('reset');
             	$("#productList").datagrid('load',{});
        	});
        	
        	
            $("#search_btn").click(function(){
            	var findcode=$("#findcode").val();
            	var findstartDate=$("#findstartDate").val();
            	var findendDate=$("#findendDate").val();
            	var findsupplierName=$("#findsupplierName").val();
            	var param={'findcode':findcode,'findstartDate':findstartDate,'findendDate':findendDate,'findsupplierName':findsupplierName,'page':1,'rows':3};
				$("#productList").datagrid({
					url:'/kxmanage/offerPriceBill/getAllByCondition',
					pageNumber:1,
					pageSize:5,
					pageList:[5,10],
					queryParams:{'findcode':findcode,'findstartDate':findstartDate,'findendDate':findendDate,'findsupplierName':findsupplierName,'page':1,'rows':3},
					pafination:true,
					checkOnSelect:false,
					columns:[[
						{field:'ck',checkbox:true},
						{field:'code',title:'报价编号',width:140,
							formatter:function(value,row,index){
								return '<a name="billCode" onclick="changeProduct(this)">'+value+'</a>';
							}
						},
						{field:'offerTime',title:'报价日期',width:120},
						{field:'customName',title:'客户名称',width:150},
						{field:'money',title:'总货值',width:60},
						{field:'linkname',title:'联系人',width:100},
						{field:'phone',title:'联系方式',width:100},
						{field:'checkStatus',title:'审核状态',width:110},
						{field:'operator',title:'操作员',width:100,
							formatter:function(value,row,index){
								return '${user.userName}';
							}
						},
						{field:'opt',title:'操作',width:200,formatter:function (value,row,index) {
							var context="<a href='#' onclick='changeProduct(this)'>修改</a>&nbsp;&nbsp;" +
							"<a href='#'>删除</a>";
							return context;
						}}
					]],
					toolbar:'#wu_toolbar',
					pagination:true ,//向后台传递参数page 和rows
					onClickRow: function(index, data) {
					    $(this).datagrid('unselectRow', index);
					}
				});
            });
        });
        function detail(id) {
            $.messager.alert("信息","查看"+id+"产品详情");
        }
        function addProduct() {
            parent.addTabs("添加报价单据","goods/addQuotationDocument.jsp");
        }
        function changeProduct(element) {
        	var code=$(element).parent().parent().parent().find("a:first").html();
            parent.addTabs("修改报价单据","/kxmanage/offerPriceBill/toChangeQuotationDocument?code="+code);
        }
        function del() {
        	var able=true;
            var rows=$("#productList").datagrid('getSelections');
          	var codes=[];
            if(rows.length==0){
                $.messager.alert('消息',"请选择记录");
                return	;
            }
            $.messager.confirm('确认','您确认想要删除记录吗?',function(r){
                if (r){
					for( var  i=0;i<rows.length;i++){
						if(rows[i].checkStatus=="已审核"){
							able=false;
						}
						codes.push(rows[i].code);
					}
					if(!able){
						$.messager.alert('消息',"不能删除已审核的记录");
						$("#productList").datagrid('load',{});
						return ;
					}else{
	                   	$.ajax({
							type : "post",//请求类型
							url:'/kxmanage/offerPriceBill/deleteOfferPriceBills',
							data :{codes:JSON.stringify(codes)},
							dataType : "json",//ajax接口（请求url）返回的数据类型
							success : function(data) {//data：返回数据（json对象）
			                    $.messager.alert('消息'," 删除成功！");
			                    $("#productList").datagrid('reload');
					            var oldCode=$("#detailed h3 span").html();
					            for( var i=0;i<codes.length;i++){
					            	if(codes[i]==oldCode){
							          	$("#show").html('<div id="detailed" style="margin-bottom:10px"></div><div id="partList" class="easyui-datagrid"></div>');
					            		break;
					            	}
					            }
							}
						});

					}
                    
                }
            });
        }
        
        function reload(){
        	parent.addTabs('报价单据管理','goods/quotationDocumentManagement.jsp')
        }
        
        function delRows(input){
          	var codes=[];
        	var code=$(input).parent().parent().parent().find("[name=billCode]").html();
            $.messager.confirm('确认','您确认想要删除记录吗?',function(r){
                if (r){
                	var able=true;
		        	var rows=$("#productList").datagrid('getRows');
		        	for(var i=0;i<rows.length;i++){
		        		if(rows[i].code==code){
		        			if(rows[i].checkStatus=="已审核"){
		        				able=false;
		        			}
		        		}
		        	}
		        	if(!able){
		        		alert("不能删除已审核的记录");
		        		$("#productList").datagrid('load',{});
		        		return ;
		        	}
                    codes.push(code);
                   $.ajax({
						type : "post",//请求类型
						url:'/kxmanage/offerPriceBill/deleteOfferPriceBills',
						data :{codes:JSON.stringify(codes)},
						dataType : "json",//ajax接口（请求url）返回的数据类型
						success : function(data) {//data：返回数据（json对象）
		                    $.messager.alert('消息'," 删除成功！");
		                    $("#productList").datagrid('reload');
				            var oldCode=$("#detailed h3 span").html();
				            if(code==oldCode){
								$("#show").html('<div id="detailed" style="margin-bottom:10px"></div><div id="partList" class="easyui-datagrid"></div>');            	
				            }
						}
					});
                }
            });
        }
    </script>

</head>
<body>
<div id="wu_toolbar">
    <div class="wu_toolbar_button">
        <a  href="#" onclick="hideTable()" class="easyui-linkbutton" data-options="iconCls:'icon-search'" plain="true">查询</a>|
        <a  href="#" onclick="addProduct()" class="easyui-linkbutton" data-options="iconCls:'icon-add'" plain="true">添加</a>|
        <a  href="#" onclick="del()" class="easyui-linkbutton" data-options="iconCls:'icon-delete-self'" plain="true">删除</a>|
        <a  href="#" onclick="reload()" class="easyui-linkbutton" data-options="iconCls:'icon-page-refresh'" plain="true">刷新</a>|
        <a  href="#" onclick="reload()" class="easyui-linkbutton" data-options="iconCls:'icon-communication'" plain="true">导出excal</a>
    </div>
    <div id="searchdiv" style="display: block;width: 100%;">
        <form name="form1" id="form1">

            <table width="100%" border="0" cellspacing="0" style="background-color:#c9dfff;" >
                <tbody><tr>
                    <td width="70"><b>检索条件:</b></td>
                    <td width="70" align="right">询价编号:</td>
                    <td width="150" align="left"><input name="findcode" class="easyui-textbox" type="text" id="findcode" value="" maxlength="100"></td>
                    <td width="70" align="right">开始日期:</td>
                    <td width="150" align="left"><div class="l-text-wrapper"><div class="l-text l-text-date"><input name="findstartDate" type="text" id="findstartDate" value="" maxlength="30" class="easyui-datebox" ligeruiid="findstartDate"><div class="l-text-l"></div><div class="l-text-r"></div><div class="l-trigger"><div class="l-trigger-icon"></div></div></div></div></td>
                    <td width="70" align="right">结束日期:</td>
                    <td width="150" align="left"><div class="l-text-wrapper"><div class="l-text l-text-date"><input name="findendDate" type="text" id="findendDate" value="" maxlength="30" class="easyui-datebox" ligeruiid="findendDate"><div class="l-text-l"></div><div class="l-text-r"></div><div class="l-trigger"><div class="l-trigger-icon"></div></div></div></div></td>
                    <td width="70" align="right">供应商名:</td>
                    <td width="150" align="left"><input name="findsupplierName" class="easyui-textbox" type="text" id="findsupplierName" value="" maxlength="100"></td>
                    <td width="72">
                        <a id="search_btn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'">搜索</a>
                    </td>
                    <td width="72">
                        <a id="reset_btn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-refresh'">重置</a>
                    </td>
                    <td width="0"></td>
                </tr>
                </tbody>
            </table>
            <input type="hidden" id="menuCode" value="100001001">
        </form>
    </div>
</div>
<div id="productList" class="easyui-datagrid">
</div>
<div id="show" style="margin-top:10px;display:none">
	<div id="detailed" style="margin-bottom:10px"></div><div id="partList" class="easyui-datagrid"></div>
</div>
</body>
</html>